﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Models.Report;
using S4Store.Domain;
using S4Store.Service;
using S4Store.Domain.Report;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using S4Store.Models;

namespace S4Store.Controllers
{
    public class ReportController : ReportBaseController
    {

        public FinancialReportService FinancialReportService { get; set; }
        public ReportService ReportService { get; set; }
        public SellerCarBrandService SellerCarBrandService { get; set; }
        public CarSalesService CarSalesService { get; set; }
        public CarService CarService { get; set; }
        public CarKpiService CarKpiService { get; set; }
        public NetMarketingKpiReportService NetMarketingKpiReportService { get; set; }
        public ExhibitionReportService ExhibitionReportService { get; set; }
        public AfterSaleCarKPIService AfterSaleCarKPIService { get; set; }
        public TelSaleReportService TelSaleReportService { get; set; }
        public EmployeeService EmployeeService { get; set; }
        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            this.InjectReportModel(model);
            model.BarModel.Action = "Index";
            model.BarModel.Controller = "Report";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(FinancialReportService);
                t.Inject(NetMarketingKpiReportService);
                t.Inject(ExhibitionReportService);
                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);

                model.SaleReport = ReportService.GetSaleReport(Month.Year, Month.Month, Store.Id);
                model.FinancialReport = FinancialReportService.GetStoreReport(Month.Year, Month.Month, Store.Id);
                model.SellerReport = ReportService.GetSellerReport(Month.Year, Month.Month, Store.Id, EmployeeType.FieldSaler);
                model.TelSellerReport = ReportService.GetSellerReport(Month.Year, Month.Month, Store.Id,  EmployeeType.TelSaler);
                //model.NetMarketingKpiReport = NetMarketingKpiReportService.GetNetMarketingKpiReport(Month.Year, Month.Month, Store.Id);
                model.ExhibitionReport = ExhibitionReportService.GetExhibitionReport(Month.Year, Month.Month, Store.Id);

                model.AfterSaleCarTypeMonthConfigs = AfterSaleCarKPIService.GetAfterSaleCarTypeMonthConfigs(Month.Year, Month.Month, Store.Id);
                model.AfterSaleCarTypeDayInputs = AfterSaleCarKPIService.GetAfterSaleCarTypeMonthFinished(Month.Year, Month.Month, Store.Id);
                int[] SAemploys = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.ServiceConsultant).Select(o => o.Id).ToArray();
                model.AFServiceAdvisorDayInputs = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorDayInputs(Month.Year, Month.Month, SAemploys);
                model.AFServiceAdvisorMonthTargets = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorMonthTargets(Month.Year, Month.Month, SAemploys);
                model.AFStandardConfigs = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs(Month.Year, Month.Month, Store.Id);

                t.Commit();
            }
            return View(model);
        }

        public ActionResult StoreChooser(StoreChooserModel model)
        {
            if (CurrentUser.Position == Position.GeneralManager ||
                CurrentUser.Position == Position.SystemManager)
            {
                model.ShowStore = true;
               
            }
            return View(model);
        }

        public ActionResult SaleTimeRate()
        {
            SaleTimeRateModel model = new SaleTimeRateModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);

                CarSaleReport report = ReportService.GetSaleReport(Month.Year, Month.Month, Store.Id);
                //if (report.IsCarKpiMonthConfigConfirmed)
                //{
                    model.DayCount = DateTime.DaysInMonth(Month.Year, Month.Month);
                    model.Day = Month.LastOrNowDay();
                    model.OrderCount = report.OrderCount;
                    model.MonthTargetOrderCount = report.OrderTarget;
                    model.SaleCount = report.GiveCarCount;
                    model.MonthTargetSaleCount = report.SaleTarget;
                    model.IsMonthConfigConfirmed = true;
               // }

                this.InjectReportModel(model);
                model.BarModel.Action = "SaleTimeRate";
                model.BarModel.Controller = "Report";
                model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='销售台次历史记录' href='" + Url.Action("SaleHistroy") + "' ><span>历史数据查询</span></a>";
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
                t.Commit();
            }
            return View(model);
        }
        
        public ActionResult SaleHistroy()
        {
            return SaleHistroy(DateTime.Now.AddMonths(-6), DateTime.Now);
        }

        [HttpPost]
        public ActionResult SaleHistroy(DateTime start,DateTime end)
        {
            SaleHistroyModel model = new SaleHistroyModel();
            start = new DateTime(start.Year, start.Month, 1);
            end = new DateTime(end.Year, end.Month, 1);

            var reports = new List<CarSaleReport>();
            for(var dt=start;dt<=end;dt=dt.AddMonths(1))
            {
                using (var t = new Transtion())
                {
                    t.Inject(ReportService);
                    CarSaleReport report = ReportService.GetSaleReport(dt.Year, dt.Month, Store.Id);
                    if (report.IsCarKpiMonthConfigConfirmed)
                    {
                        reports.Add(report);
                    }
                    model.Reports.Add(report);
                    t.Commit();
                }
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "SaleHistroy";
            model.BarModel.Controller = "Report";
            //model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            model.BarModel.Start = start;
            model.BarModel.End = end;
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            return View(model);
        }

        public ActionResult CarBrandTimeRate()
        {
            CarBrandTimeRateModel model = new CarBrandTimeRateModel();
            model.DayCount = DateTime.DaysInMonth(Month.Year, Month.Month);
            model.Day = Month.LastOrNowDay();            
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(CarKpiService);
                CarSaleReport report = ReportService.GetSaleReport(Month.Year, Month.Month, Store.Id);
                if (report.IsCarKpiMonthConfigConfirmed)
                {
                    model.IsMonthConfigConfirmed = true;
                }
                var brands = CarKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                foreach (var brand in brands)
                {
                    var CarBrandSaleReport = ReportService.GetCarBrandSaleReport(Month.Year, Month.Month, Store.Id, brand.Id);
                    model.Reports.Add(CarBrandSaleReport);
                    //S4Store.Models.Report.CarBrandTimeRateModel.BrandData data = new S4Store.Models.Report.CarBrandTimeRateModel.BrandData();
                    //data.CarBrand = brand;
                    //data.Sale = CarBrandSaleReport.GiveCarCount;
                    //data.SaleTarget = CarBrandSaleReport.SaleTarget;
                    //data.Rate = CarBrandSaleReport.SaleRate;
                    //data.DateRate = model.DateRate;
                    //model.BrandDatas.Add(data);
                }
                model.Report = ReportService.GetSaleReport(Month.Year, Month.Month, Store.Id);
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "CarBrandTimeRate";
            model.BarModel.Controller = "Report";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult CarModelTimeRate()
        {
            CarModelTimeRateModel model = new CarModelTimeRateModel();
            model.DayCount = DateTime.DaysInMonth(Month.Year, Month.Month);
            model.Day = Month.LastOrNowDay();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(CarService);
                t.Inject(CarKpiService);
                CarSaleReport report = ReportService.GetSaleReport(Month.Year, Month.Month, Store.Id);
                if (report.IsCarKpiMonthConfigConfirmed)
                {
                    model.IsMonthConfigConfirmed = true;
                }
                var models = CarKpiService.GetCarmodelsForStoreMonth(Month.Year, Month.Month, Store.Id, SearchCarBrandId.Value).ToList();
                foreach (var cm in models)
                {
                    var CarModelSaleReport = ReportService.GetCarModelSaleReport(Month.Year, Month.Month, Store.Id, cm.Id);
                    model.Reports.Add(CarModelSaleReport);
                    //S4Store.Models.Report.CarModelTimeRateModel.ModelData data = new S4Store.Models.Report.CarModelTimeRateModel.ModelData();
                    //data.CarModel = cm;
                    //data.Sale = CarModelSaleReport.GiveCarCount;
                    //data.SaleTarget = CarModelSaleReport.SaleTarget;
                    //data.Rate = CarModelSaleReport.SaleRate;
                    //model.ModelDatas.Add(data);
                }

            }
            this.InjectReportModel(model);
            model.BarModel.Action = "CarModelTimeRate";
            model.BarModel.Controller = "Report";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            model.BarModel.PlusItem = "<input type='hidden' name='CarBrandId' value='" + SearchCarBrandId.Value + "'> <a target='navtab' class='historyrecord' title='分车款销量时间历史数据' href='" + Url.Action("CarModelHistory", new { SearchCarBrandId = SearchCarBrandId.Value }) + "' ><span>历史数据查询</span></a>";
            model.BrandId = SearchCarBrandId.Value;
            return View(model);
        }

        public ActionResult SaleHealthy()
        {
            SaleHealthyModel model = new SaleHealthyModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(TelSaleReportService);
                CarSaleReport report = ReportService.GetSaleReport(Month.Year, Month.Month, Store.Id);
                var lastmonth = Month.AddMonths(-1);
                CarSaleReport lastreport = ReportService.GetSaleReport(lastmonth.Year, lastmonth.Month, Store.Id);
                var telsalereport = TelSaleReportService.GetStoreReport(Month.Year, Month.Month, Store.Id);
                if (report.IsCarKpiMonthConfigConfirmed)
                {
                    model.IsMonthConfigConfirmed = true;
                    
                    model.销量时间进度动态 = report.SaleRateDiff;
                    model.新车库存结构动态 = report.InventoryCoefficientDiff;
                    //if (lastreport == null || !lastreport.IsCarKpiMonthConfigConfirmed)
                    //{
                    //    model.新车库存结构动态 = report.InventoryCoefficient;
                    //}
                    //else
                    //{
                    //    model.新车库存结构动态 = report.InventoryCoefficient - lastreport.InventoryCoefficient;
                    //}
                    model.新车投保业绩动态 = report.TBDiff;
                    model.新车延保业绩动态 = report.YBDiff;
                    model.新车置换业绩动态 = report.ZHDiff;
                    model.金融贷款业绩动态 = report.AJDiff;
                    model.精品装潢业绩动态 = report.PackageDiff;
                    model.电话销售业绩动态 = telsalereport.XSSJJDC;
                }
            }




            this.InjectReportModel(model);
            model.BarModel.Action = "SaleHealthy";
            model.BarModel.Controller = "Report";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        
        public ActionResult CarModelHistory()
        {
            CarModelHistoryModel model = new CarModelHistoryModel();
            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, 1);
            }

            var reports = new List<CarSaleCommonReport>();
            for (var dt = start; dt <= end; dt = dt.AddMonths(1))
            {
                using (var t = new Transtion())
                {
                    t.Inject(ReportService);
                    CarSaleCommonReport report;
                    if (SearchCarModelId == null || SearchCarModelId.Value == 0)
                    {
                        report = ReportService.GetCarBrandSaleReport(dt.Year, dt.Month, Store.Id, SearchCarBrandId.Value);
                    }
                    else
                    {
                        report = ReportService.GetCarModelSaleReport(dt.Year, dt.Month, Store.Id, SearchCarModelId.Value);
                    }
                    //if (report.IsCarKpiMonthConfigConfirmed)
                    //{
                        reports.Add(report);
                    //}
                    t.Commit();
                }
            }
            model.Reports=reports;
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                t.Inject(CarKpiService);
                var brand = CarService.GetCarBrandQuery().FirstOrDefault(x => x.Id == SearchCarBrandId);
                model.CarBrandName = brand.Name;
                model.CarModels = CarKpiService.GetCarmodelsForStoreMonths(start, end, Store.Id, SearchCarBrandId.Value).ToList();
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "CarModelHistory";
            model.BarModel.Controller = "Report";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.Start = start;
            model.BarModel.End = end;
            model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='分车款销量时间历史数据' href='" + Url.Action("CarModelHistory", new { CarBrandId = SearchCarBrandId }) + "' ><span>历史数据查询</span></a>";

            model.CarBrandId = SearchCarBrandId.Value;
            model.CarModelId = SearchCarModelId == null ? 0 : SearchCarModelId.Value;
            return View(model);
        }


        public ActionResult CarBrandOrderTimeRate()
        {
            CarBrandOrderTimeRateModel model = new CarBrandOrderTimeRateModel();
            model.DayCount = DateTime.DaysInMonth(Month.Year, Month.Month);
            model.Day = Month.LastOrNowDay();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(CarKpiService);
                CarSaleReport report = ReportService.GetSaleReport(Month.Year, Month.Month, Store.Id);
                if (report.IsCarKpiMonthConfigConfirmed)
                {
                    model.IsMonthConfigConfirmed = true;
                }
                var brands = CarKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                foreach (var brand in brands)
                {
                    var CarBrandSaleReport = ReportService.GetCarBrandSaleReport(Month.Year, Month.Month, Store.Id, brand.Id);
                    S4Store.Models.Report.CarBrandOrderTimeRateModel.BrandData data = new S4Store.Models.Report.CarBrandOrderTimeRateModel.BrandData();
                    data.CarBrand = brand;
                    data.Sale = CarBrandSaleReport.OrderCount;
                    data.SaleTarget = CarBrandSaleReport.OrderTarget;
                    data.Rate = CarBrandSaleReport.OrderRate;
                    data.DateRate = model.DateRate;
                    model.BrandDatas.Add(data);
                }
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "CarBrandOrderTimeRate";
            model.BarModel.Controller = "Report";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }


        public ActionResult CarModelOrderTimeRate()
        {
            CarModelOrderTimeRateModel model = new CarModelOrderTimeRateModel();
            model.DayCount = DateTime.DaysInMonth(Month.Year, Month.Month);
            model.Day = Month.LastOrNowDay();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(CarService);
                t.Inject(CarKpiService);
                CarSaleReport report = ReportService.GetSaleReport(Month.Year, Month.Month, Store.Id);
                if (report.IsCarKpiMonthConfigConfirmed)
                {
                    model.IsMonthConfigConfirmed = true;
                }
                var models = CarKpiService.GetCarmodelsForStoreMonth(Month.Year, Month.Month, Store.Id, SearchCarBrandId.Value).ToList();
                foreach (var cm in models)
                {
                    var CarModelSaleReport = ReportService.GetCarModelSaleReport(Month.Year, Month.Month,Store.Id, cm.Id);
                    //S4Store.Models.Report.CarModelOrderTimeRateModel.ModelData data = new S4Store.Models.Report.CarModelOrderTimeRateModel.ModelData();
                    //data.CarModel = cm;
                    //data.Sale = CarModelSaleReport.OrderCount;
                    //data.SaleTarget = CarModelSaleReport.OrderTarget;
                    //data.Rate = CarModelSaleReport.OrderRate;
                    model.Reports.Add(CarModelSaleReport);
                }
                model.CarBrand = CarService.GetCarBrandQuery().FirstOrDefault(x => x.Id == SearchCarBrandId.Value);
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "CarModelOrderTimeRate";
            model.BarModel.Controller = "Report";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
			if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.SaleTutors ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
            model.BarModel.PlusItem = "<ul class='toolBar'><li><a class='historyrecord edit' backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.Sale.ToInt() ,referdesc="销售管理>销售时间进度动态>车型查询>订单进度" }) + "' target='dialog' width='30%'><span class='icon edit'>填写改善指导建议</span></a></li></ul>";
		}
            
            //model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='分车款订单时间历史数据' href='" + Url.Action("CarModelHistory", new { SearchCarBrandId = SearchCarBrandId.Value }) + "' ><span>历史数据查询</span></a>";
           

            return View(model);
        }

    }
}
